package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.model.AlarmEntity;
import com.hualude.roadsign.model.IotcmdMsgEntity;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.AlarmService;
import com.hualude.roadsign.utils.FormatNumber;
import com.hualude.roadsign.utils.UdpSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/10/13.
 */
@Service
public class AlarmServiceImpl implements AlarmService {
    @Resource
    private AlarmRepository alarmRepository;

    @Resource
    private PortRepository portRepository;

    @Resource
    private PacketInfoRepository packetInfoRepository;

    @Resource
    private IotcmdMsgRepository iotcmdMsgRepository;

    @Resource
    private SignalLampRepository signalLampRepository;

    @Resource
    private SignalMachineRepository signalMachineRepository;

    @Resource
    private SolarEnergyRepository solarEnergyRepository;

    @Resource
    private HighLeverRepository highLeverRepository;

    @Resource
    private MonitorRepository monitorRepository;

    @Resource
    private RuleGroupRepository ruleGroupRepository;

    @Resource
    private AlarmPushRepository alarmPushRepository;


    @Override
    public DTResponseDto getAlarm4DT(DTRequestDto dtRequestDto, Integer orgId, Integer actorId, Integer deviceType) throws Exception {
        List<AlarmDto> alarmDto = alarmRepository.getAlarm4DT(dtRequestDto, orgId, actorId, deviceType);
        Integer count = alarmRepository.getAlarmCount4DT(orgId, actorId, deviceType);
        List<RuleGroupDto> groupDtos = ruleGroupRepository.getRuleGroupIdAndName();
        HashMap<Integer, String> groupIdNameMap = new HashMap<>();
        for (RuleGroupDto item : groupDtos) {
            groupIdNameMap.put(item.getRuleGroupId(), item.getRuleGroupName());
        }
        for (AlarmDto item : alarmDto) {
            item.setRuleGroupName(groupIdNameMap.get(Integer.parseInt(item.getDeviceModel())));
        }
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, alarmDto);
    }

    @Override
    public DTResponseDto getAlarmBySearch4DT(DTRequestDto dtRequestDto, String orgId, String deviceId, String createTime, Integer deviceType) throws Exception {
        List<AlarmDto> alarmDtos = alarmRepository.getAlarmBySearch4DT(dtRequestDto, orgId, deviceId, createTime, deviceType);
        Integer count = alarmRepository.getAlarmCountBySearch4DT(dtRequestDto, orgId, deviceId, createTime, deviceType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, alarmDtos);
    }

    @Override
    public List<PortDto> getPortById(String deviceId) throws Exception {
        return portRepository.getPortById(deviceId);
    }

    @Override
    @Transactional
    public Integer deletePortById(Integer portId) throws Exception {
        return portRepository.deletePortById(portId);
    }

    @Override
    @Transactional
    public Integer addPort4Alarm(PortDto portDto) throws Exception {
        return portRepository.save(portDto.toBean()).getPortId();
    }

    @Override
    @Transactional
    public Integer updatePort(PortDto portDto) throws Exception {
        return portRepository.updatePort(portDto.getPortId(), portDto.getPortNum(), portDto.getPortState(), portDto.getPortMeterValue(), portDto.getPortMeterMin(), portDto.getPortMeterMax(), portDto.getCreateTime(), portDto.getUpdateTime());
    }

    @Override
    @Transactional
    public Integer sendIoCmd(String deviceId, String cmd, Integer cycleTime) throws Exception {
        if(deviceId==null)deviceId="00000";
        IotcmdMsgEntity iotcmdMsgEntity = new IotcmdMsgEntity(deviceId, cmd, FormatNumber.getTime(), FormatNumber.getTime(), 0);
        UdpSender.getInstance().sendMsg(cmd);
        if(cmd.contains("setpara")){
            alarmRepository.setCycleTime(deviceId, cycleTime);
            UdpSender.getInstance().sendMsg(deviceId + ",UPDATEALARM");
        }
        return iotcmdMsgRepository.save(iotcmdMsgEntity).getCmdMsgId();
    }

    @Override
    public DTResponseDto getDeviceRecord4DT(DTRequestDto dtRequestDto, String deviceId, String infoType) throws Exception {
        List<PacketInfoDto> dtos = packetInfoRepository.getDeviceRecord4DT(dtRequestDto, deviceId, infoType);
        Integer count = packetInfoRepository.getDeviceRecordCount4DT(dtRequestDto.search, deviceId, infoType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public List<AlarmDto> getAlarmPath(String deviceId) throws Exception {
        return alarmRepository.getAlarmPath(deviceId);
    }

    @Override
    @Transactional
    public Integer addAlarm(AlarmDto alarmDto) throws Exception {
        alarmDto.setCreateTime(FormatNumber.getTime());
        alarmDto.setUpdateTime(FormatNumber.getTime());
        alarmDto.setCycleTime(120);
        return alarmRepository.save(alarmDto.toBean()).getAlarmId();
    }

    @Override
    @Transactional
    public Integer updateAlarm(AlarmDto alarmDto) throws Exception {
        AlarmEntity old = alarmRepository.findOne(alarmDto.getAlarmId());
        Integer result = alarmRepository.updateAlarm(alarmDto.getDeviceId(), alarmDto.getDeviceState(), alarmDto.getDeviceModel(), alarmDto.getDeviceManufacturer(), alarmDto.getDeviceAddress(), FormatNumber.getTime(), alarmDto.getAlarmId(), alarmDto.getCycleTime(), alarmDto.getSpecialPoint(), alarmDto.getDeviceOrgId());
        if (result > 0) {
            if (!old.getDeviceModel().equals(alarmDto.getDeviceModel())) {
                UdpSender.getInstance().sendMsg(alarmDto.getDeviceId() + ",UPDATEMODEL");
            } else {
                UdpSender.getInstance().sendMsg(alarmDto.getDeviceId() + ",UPDATEALARM");
            }

        }
        return result;
    }


    @Override
    @Transactional
    public Integer deleteAlarmById(String alarmIds) throws Exception {
        String[] alarmIdArray = alarmIds.split(",");
        for (String alarmId : alarmIdArray) {
            alarmRepository.deleteAlarmById(Integer.parseInt(alarmId));
        }
        return alarmIdArray.length;
    }

    @Override
    public List<AlarmDto> getAlarmList(Integer orgId) throws Exception {
        return alarmRepository.getAlarmList(orgId);
    }



    @Override
    public AlarmPushDto getLastAlarmPushByDeviceId(String deviceId) throws Exception {
        return alarmPushRepository.getLastAlarmPushByDeviceId(deviceId);
    }
}
